<template>
  <div class="login">
    <h1>欢迎加入iKun大家庭!!!</h1>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item
        label="账号"
        prop="user_name"
      >
        <el-input v-model="ruleForm.user_name"></el-input>
      </el-form-item>
      <el-form-item
        label="密码"
        prop="password"
      >
        <el-input
          v-model="ruleForm.password"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          >立即登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <div class="iKunimg"></div>
    </el-form>
  </div>
</template>
<script>
import { login } from '@/api/login';
import { nanoid } from 'nanoid';
export default {
  data() {
    return {
      ruleForm: {
        user_name: '17673841851',
        password: '958611064',
      },
      rules: {
        user_name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    open4() {
      this.$message.error('请按正确的格式输入~');
    },
    open2() {
      this.$message({
        message: '登录成功~',
        type: 'success',
      });
    },
    login() {
      login(this.ruleForm.user_name, this.ruleForm.password).then((res) => {
        window.sessionStorage.setItem('tockenstr', nanoid());
        this.open2();
        console.log(res.headers.set_cookie);
        console.log(res.data.success);
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login();
          setTimeout(() => {
            this.$router.push('/');
          }, 500);
        } else {
          this.open4();
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  background: url('../../public/imgs/登录背景图.gif') no-repeat 0 0/100% 100%;
  overflow: hidden;
  h1 {
    width: 100%;
    text-align: center;
    color: #fff;
    font-size: 20px;
    padding-top: 50px;
  }
  .demo-ruleForm {
    margin: 140px auto 0;
    width: 400px;
    height: auto;
    background-color: rgba(255, 255, 255, 0.7);
    padding: 50px 50px 30px 0;
    border-radius: 30px;
    position: relative;
    .iKunimg {
      height: 80px;
      width: 80px;
      background: url('../../public/imgs/iKun.webp') no-repeat 0 0/100% 100%;
      border-radius: 50%;
      position: absolute;
      top: -40px;
      left: -40px;
    }
  }
}
</style>
